{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "absolute-basketball",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/most-profit-assigning-work/\n",
    "\n",
    "\n",
    "\n",
    "It's working on my local computer, but gettings an run-time error on leetcode...\n",
    "\n",
    "I don't know where goes wrong.\n",
    "\n",
    "\n",
    "\n",
    "```c++\n",
    "#include <vector>\n",
    "#include <algorithm>\n",
    "#include <map>\n",
    "#include <iostream>\n",
    "\n",
    "using namespace std;\n",
    "\n",
    "\n",
    "bool sortbysec(const tuple<int, int, int>& a, \n",
    "               const tuple<int, int, int>& b)\n",
    "{\n",
    "    return (get<1>(a) < get<1>(b));\n",
    "}\n",
    "\n",
    "class Solution {\n",
    "public:\n",
    "    int binary_search(vector<int> arr, int low, int high, int x) {\n",
    "        int mid;\n",
    "        if (high >= low) {\n",
    "            mid = (high + low) / 2;\n",
    "            if (arr[mid] == x) {\n",
    "                while (mid + 1 < arr.size()) {\n",
    "                    if (arr[mid+1] > x) {\n",
    "                        return mid;\n",
    "                    }\n",
    "                    mid += 1;\n",
    "                }\n",
    "            } else if (arr[mid] > x) {\n",
    "                return binary_search(arr, low, mid - 1, x);\n",
    "            } else {\n",
    "                return binary_search(arr, mid+1, high, x);\n",
    "            }\n",
    "            return high;\n",
    "        } else {\n",
    "            return high;\n",
    "        }\n",
    "    }\n",
    "    int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {\n",
    "        //6:18\n",
    "        int length = difficulty.size();\n",
    "        vector<tuple<int, int, int>> info_arr;\n",
    "        for (int i=0; i < length; i++) {\n",
    "            info_arr.push_back(make_tuple(i, difficulty[i], profit[i]));\n",
    "        }\n",
    "        sort(info_arr.begin(), info_arr.end(), sortbysec);\n",
    "        vector<int> new_difficulty;\n",
    "        vector<int> new_profit;\n",
    "        for(int i=0; i < length; i++) {\n",
    "            new_difficulty.push_back(get<1>(info_arr[i]));\n",
    "            new_profit.push_back(get<2>(info_arr[i]));\n",
    "        }\n",
    "        int money = 0;\n",
    "        sort(worker.begin(), worker.end());\n",
    "        for (auto wk : worker) {\n",
    "            int index = binary_search(new_difficulty, 0, length-1, wk);\n",
    "            if (new_difficulty[index] <= wk) {\n",
    "                money += *max_element(new_profit.begin(), new_profit.begin()+index+1);\n",
    "            }\n",
    "        }\n",
    "        return money;\n",
    "        //6:38\n",
    "    }\n",
    "};\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xcpp17"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
